//
//  FMDBTransactionHistory.m
//  takeout
//
//  Created by Mac on 12-5-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FMDBTransactionHistory.h"
#import "FMDatabase.h"
#import "FMDBManager.h"
#import "Supplier.h"
#import "TransactionHistory.h"
#import "Constants.h"
@implementation FMDBTransactionHistory
//插入浏览纪录
-(void) insertHistory:(Supplier *)supplier{
    NSInteger count=0;
    //不同硬件的数量
    NSInteger tempCount=SSiphoneType==0?8:12;

    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    FMResultSet *rs = [db executeQuery:@"SELECT count(*) FROM transaction_historys"];
    while ([rs next]) {
        count=[rs intForColumnIndex:0];
    }
    FMResultSet *isExistRs=[db executeQuery:@"SELECT * FROM transaction_historys where supplierid=?",[NSNumber numberWithInt:supplier.supplierId]];
    //判断是否存在
    if(![isExistRs next])
    {
        if(count<tempCount){
            [db executeUpdate:@"INSERT INTO transaction_historys(supplierid,name,picture) values(?,?,?)",[NSNumber numberWithInt:[supplier supplierId]],supplier.supplierName,supplier.imgUrl];
        }
        else{
            NSInteger firstId=0;
            FMResultSet *firstIdRs = [db executeQuery:@"SELECT * FROM transaction_historys order by id limit 1"];
            while ([firstIdRs next]) {
                firstId=[firstIdRs intForColumn:@"id"];
            }
            [db executeUpdate:@"delete from transaction_historys where id=?",[NSNumber numberWithInt:firstId]];
            [db executeUpdate:@"INSERT INTO transaction_historys(supplierid,name,picture) values(?,?,?)",[NSNumber numberWithInt:[supplier supplierId]],supplier.supplierName,supplier.imgUrl];
        }
    }
    else{
        //有重复的店铺,删除掉原来的，插入到最新
        [db executeUpdate:@"delete from transaction_historys where supplierid=?",[NSNumber numberWithInt:supplier.supplierId]];
        [db executeUpdate:@"INSERT INTO transaction_historys(supplierid,name,picture) values(?,?,?)",[NSNumber numberWithInt:[supplier supplierId]],supplier.supplierName,supplier.imgUrl];
    }
//    [fmdbManager closeDataBase];
}
//获取所有浏览纪录
-(NSArray *) getAllHistory{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
//  [db executeUpdate:@"delete from transaction_historys"];
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM transaction_historys order by id desc"];
    
//    NSMutableArray *transactionHistorys=[[NSMutableArray alloc] initWithObjects:@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",nil];
//    NSMutableArray *transactionHistorys=[NSArray arrayWithObjects:@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1", nil];
    
    
    NSMutableArray *transactionHistorys=SSiphoneType==0?[NSMutableArray arrayWithObjects:@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1", nil]:[NSMutableArray arrayWithObjects:@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",nil];

    int i=0;
    while ([rs next]) {
        TransactionHistory *transactionHistory=[[TransactionHistory alloc]init];
        transactionHistory.historyId=[rs intForColumn:@"id"];
        transactionHistory.supplierId=[rs intForColumn:@"supplierid"];
        transactionHistory.supplierName=[rs stringForColumn:@"name"];
        transactionHistory.imgUrl=[rs stringForColumn:@"picture"];
        [transactionHistorys replaceObjectAtIndex:i withObject:transactionHistory];
        i++;
    }
    [rs close];
    return transactionHistorys;
}
//清除所有浏览纪录
-(void) deleteAllHistory{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    [db executeUpdate:@"DELETE FROM transaction_historys"];
//    [fmdbManager closeDataBase];
}
@end
